package com.retail.proxy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.retail.proxy.entity.vo.ProxyMechanismCategoryVo;
import com.retail.proxy.entity.vo.ProxyMechanismVo;
import com.retail.core.entity.ParamReq;
import com.retail.proxy.entity.request.ProxyMechanismRequest;
import com.retail.proxy.mapper.ProxyMechanismCategoryMapper;
import com.retail.proxy.mapper.ProxyMechanismMapper;
import com.retail.proxy.service.ProxyMechanismService;
import org.apache.ibatis.annotations.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 代理机制 Service impl
 *
 * @author Administrator
 */
@Service
public class ProxyMechanismServiceImpl implements ProxyMechanismService {

    @Resource
    private ProxyMechanismMapper proxyMechanismMapper;

    @Resource
    private ProxyMechanismCategoryMapper proxyMechanismCategoryMapper;

    @Override
    public ProxyMechanismVo getModel(Long id) {
        ProxyMechanismVo vo = proxyMechanismMapper.getModel(id);
        ProxyMechanismCategoryVo categoryVo = proxyMechanismCategoryMapper.getModel(vo.getProxyLevelTitle());
        if (ObjectUtil.isNotEmpty(categoryVo)) {
            vo.setProxyLevelTitleName(categoryVo.getCategoryName());
        }

        return vo;
    }

    @Transactional
    @Override
    public Long save(ProxyMechanismRequest proxyMechanismRequest) {
        proxyMechanismMapper.save(proxyMechanismRequest);
        return proxyMechanismRequest.getId();
    }

    @Transactional
    @Override
    public Long update(ProxyMechanismRequest proxyMechanismRequest) {
        proxyMechanismMapper.update(proxyMechanismRequest);
        return proxyMechanismRequest.getId();
    }

    @Override
    public List<ProxyMechanismVo> listPage(ParamReq<ProxyMechanismRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<ProxyMechanismVo> res = proxyMechanismMapper.listPage(params);
        res.forEach(proxyMechanismVo -> {
            ProxyMechanismCategoryVo categoryVo = proxyMechanismCategoryMapper.getModel(proxyMechanismVo.getProxyLevelTitle());
            if (ObjectUtil.isNotEmpty(categoryVo)) {
                proxyMechanismVo.setProxyLevelTitleName(categoryVo.getCategoryName());
            }
        });

        return res;
    }

    @Override
    public Integer countPage(ParamReq<ProxyMechanismRequest> params) {
        return proxyMechanismMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return proxyMechanismMapper.updateDelFlagById(id, delFlag);
    }
}
